import React,{useState,useEffect} from 'react'
import store from '../../redux/store'
import {incrementAction,decreaseAction} from '../../redux/action'
export default function Counter() {
  let [n,setN]=useState(0)
  console.log('store',store);
  const increment=()=>{
    store.dispatch(incrementAction(5))
    console.log(store.getState());

  }
  const decrease=()=>{
    store.dispatch(decreaseAction(3))
  }
  useEffect(()=>{
    //订阅方法
    store.subscribe(()=>{
        setN(Math.random())
    })
  })
  return (
    <div>
        <h1>计数器:{store.getState().num}</h1>
        <button onClick={()=>{increment()}}>+</button>
        <button onClick={()=>{decrease()}}>-</button>
        <button onClick={()=>{store.dispatch(incrementAction(8))}}>+8</button>
    </div> 
  )
}
